﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using ShareMarking.Interface;
using ShareMarking.Entity;
using ShareMarking.Service;
using MySoft.Data;
using System.Data;

namespace ShareMarking.Web
{
    public partial class TestNC : System.Web.UI.Page
    {
        public string nccode = "";
        public string ncname = "";
        public string code_url = "";
        public string mobile = "离职员工";

        public string nclcode = "离职员工";
        public string nclname = "离职员工";
        public string nclmobile = "离职员工";

        public string shopcode = "";
        public string shopname = "";
        public string shophtml = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string Hidden = HfTest.Value;
                Label1.Text = Hidden;
                Response.Write(Label1.Text);  
            }                              
        }
        //方法001 创建单图文
        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            ComLib.LogLib.Log4NetBase.Log("开始新增一个单图文NC数据！");
            string httpstr = ConfigurationManager.AppSettings["nchttp"];
            string access_key_id = ConfigurationManager.AppSettings["access_key_id"];
            string access_key_secret = ConfigurationManager.AppSettings["access_key_secret"];
            var ncsync = new NC_Sync(httpstr, access_key_id, access_key_secret);

            #region 新增NC数据
            string action = "one";
            string code = "";
            if (!nc_code.Text.Trim().IsNullOrEmpty())
            {
                //创建NC二维码
                code = nc_code.Text.Trim();
                ComLib.LogLib.Log4NetBase.Log("1、==========================创建单图文带NC信息二维码========开始====");
                string vigohttp = ConfigurationManager.AppSettings["vigohttp"];
                string signscrect = ConfigurationManager.AppSettings["signscrect"];
                NC_QRCode ncqrsync = new NC_QRCode(vigohttp, signscrect);
                string msg = ncqrsync.Sync_QrCodeAdd(action, code);
                txtMessage.Text = msg;
                ComLib.LogLib.Log4NetBase.Log("2、==========================创建单图文带NC信息二维码========结束====");
            }
            else {

                //创建NC二维码
                ComLib.LogLib.Log4NetBase.Log("1、==========================创建单图文NC二维码========开始====");
                string vigohttp = ConfigurationManager.AppSettings["vigohttp"];
                string signscrect = ConfigurationManager.AppSettings["signscrect"];
                NC_QRCode ncqrsync = new NC_QRCode(vigohttp, signscrect);
                string msg = ncqrsync.Sync_QrCodeAdd(action, code);
                txtMessage.Text = msg;
                ComLib.LogLib.Log4NetBase.Log("2、==========================创建单图文NC二维码========结束====");

            }

            
            #endregion
        }

        private void Sync(NC_Sync ncsync)
        {
            //先获取所有NC数量
            var count = ncsync.GetNCCount();
            ComLib.LogLib.Log4NetBase.Log("1、====同步NC数据====获取NC数量为：：" + count);

            //设置50页一次
            var pagesize = 50;
            var currentpage = 0;
            var nextpage = 1;
            var pagecout = (int)(count / pagesize);
            for (int i = 0; i < pagecout; i++)
            {
                //获取一页数据
                var resultentity = ncsync.GetNC(currentpage, nextpage, pagesize, count);
                ComLib.LogLib.Log4NetBase.Log("2、====同步NC数据====获取第++" + i + "++页数据：：state:"
                            + resultentity.state + ":::msg:::" + resultentity.message);

                var enlit = resultentity.result.list;
                enlit.ForEach(o =>
                {
                    try
                    {
                        #region 处理一条数据
                        if (!nc_ncuserService.Instance.Get_IsExist_ByWhere(ShareMarking.Entity.nc_ncuser._.nc_code == o.nc_code))
                        {
                            #region 新增NC
                            ShareMarking.Entity.nc_ncuser nc = new ShareMarking.Entity.nc_ncuser()
                            {
                                id = o.id.ToString(),
                                nc_code = o.nc_code,
                                password = o.password,
                                name = o.name,
                                tel = o.tel,
                                email = o.email,
                                state = o.state,
                                nc_nature = o.nc_nature,
                                entr_date = o.entr_date,
                                leave_date = o.leave_date,
                                ncl_code = o.ncl_code,
                                ncl_name = o.ncl_name,
                                ncl_tel = o.ncl_tel,
                                isneedupdate = true
                            };
                            nc_ncuserService.Instance.Add_Entity(nc);
                            

                            ComLib.LogLib.Log4NetBase.Log("2、=======新增一条NC====NCCODE:" + o.nc_code + "=======NC状态======" + o.state);

                            #endregion
                        }
                        else
                        {
                            #region 更新NC
                            var nc = nc_ncuserService.Instance.Get_SingleEntity_byWhere(
                                    ShareMarking.Entity.nc_ncuser._.nc_code == o.nc_code);
                            var oldstate = nc.state;
                            if (oldstate != o.state)//必须在前，在后面会覆盖值
                            {
                                nc.isneedupdate = true;
                            }
                            else
                            {
                                nc.isneedupdate = false;
                            }
                            nc.name = o.name;
                            nc.tel = o.tel;
                            nc.email = o.email;
                            nc.state = o.state;
                            nc.nc_nature = o.nc_nature;
                            nc.entr_date = o.entr_date;
                            nc.leave_date = o.leave_date;
                            nc.ncl_code = o.ncl_code;
                            nc.ncl_name = o.ncl_name;
                            nc.ncl_tel = o.ncl_tel;

                            nc_ncuserService.Instance.Update_Entity(nc);

                            ComLib.LogLib.Log4NetBase.Log("2、=========更新NC数据====NCCODE:" + o.nc_code +
                                "====新的状态为:" + nc.state +
                                "====旧的状态为:" + oldstate +
                                "====是否需要更新:" + nc.isneedupdate);

                            #endregion
                        }
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        ComLib.LogLib.Log4NetBase.Log("2、====同步NC数据====错误=====" + ex.Message + ex.StackTrace);
                    }
                });

                currentpage += 1;
                nextpage += 1;
                ComLib.LogLib.Log4NetBase.Log("2、====同步NC数据====处理第++" + i + "++页数据结束");
            }
            ComLib.LogLib.Log4NetBase.Log("2、====同步NC数据========结束====");
        }

        public string shownc(string nccode)
        {
            //string nccode = ComLib.QueryStringHelper.GetFormString("nccode");
            ShareMarking.Entity.nc_ncuser nc = nc_ncuserService.Instance.Get_SingleEntity_byWhere(ShareMarking.Entity.nc_ncuser._.nc_code == nccode);
            if (nc != null)
            {
                qr_QrCodeRelate relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
                    qr_QrCodeRelate._.rel_Code == nccode && qr_QrCodeRelate._.IsDelete == false);
                if (relate != null)
                {
                    qr_QrCode qr = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.scene_id == relate.qr_Scene_ID);
                    if (qr != null)
                    {
                        ncname = nc.name;
                        code_url = qr.qrcode_url;
                        mobile = nc.tel;
                        nclcode = nc.ncl_code;
                        nclname = nc.ncl_name;
                        nclmobile = nc.ncl_tel;
                    }
                }
            }
            return code_url;
        }
        //方法002 更新二维码
        protected void Button2_Click(object sender, EventArgs e)
        {
            ComLib.LogLib.Log4NetBase.Log("开始更新一个二维码数据！");
            string httpstr = ConfigurationManager.AppSettings["nchttp"];
            string access_key_id = ConfigurationManager.AppSettings["access_key_id"];
            string access_key_secret = ConfigurationManager.AppSettings["access_key_secret"];
            var ncsync = new NC_Sync(httpstr, access_key_id, access_key_secret);

            #region 更新NC数据
            string code = "";

            //创建NC二维码
            ComLib.LogLib.Log4NetBase.Log("3、==========================更新NC二维码========开始====");
            string vigohttp = ConfigurationManager.AppSettings["vigohttp"];
            string signscrect = ConfigurationManager.AppSettings["signscrect"];
            NC_QRCode ncqrsync = new NC_QRCode(vigohttp, signscrect);
            string msg = ncqrsync.Sync_QrCode(code);
            txtMessage.Text = msg;
            ComLib.LogLib.Log4NetBase.Log("3、==========================更新NC二维码========结束====");
            #endregion
        }

        //方法004 根据code或scene_id查询对应二维码绑定的图文
        protected void Button3_Click(object sender, EventArgs e)
        {
            if (txtScene_id.Text.IsNullOrEmpty())
            {
                txtMessage.Text = "请输入scene_id";
            }
            else
            {

                ComLib.LogLib.Log4NetBase.Log("---------------开始检查NC！");
                try
                {
                    #region 检查NC
                   // #region check
                   //Qrcode_vigo vigo = new Qrcode_vigo("http://wx.meadjohnson.com.cn/API/Qrcode/index", "sdf5sf5et5e42t5");
                   // //var imgurl = table.Rows[i]["qrcode_url"].ToString();
                   // //var qrcodecontent = check.getqrcontent(imgurl);
                   //var resultwx = vigo.GetWXContent("", txtScene_id.Text);

                   // var vigomessage0 = "title:" + resultwx.message[0].title
                   //     + "desc:" + resultwx.message[0].shortdesc
                   //     + "pic:" + resultwx.message[0].picurl
                   //     + "link:" + resultwx.message[0].linkurl;
                   // var message0 = "title:美赞臣蓝臻，突破性贴近宝宝需求，新妈买一赠一限量臻享"
                   //         + "desc:"
                   //         + "pic:http://qrcode.mjn.howjoin.com/imgforweixin/ac419.jpg"
                   //         + "link:" + "http://wx.ismartgo.com/app/lighthouse/index.aspx?from="
                   //         + "123" + "$type=1$openid=####";
                   // var vigomessage1 = "title:" + resultwx.message[1].title
                   //     + "desc:" + resultwx.message[1].shortdesc
                   //     + "pic:" + resultwx.message[1].picurl
                   //     + "link:" + resultwx.message[1].linkurl;
                   // var message1 = "title:马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇"
                   //         + "desc:一键注册，终身礼遇"
                   //         + "pic:http://admin.mjn.howjoin.com/images/p4.jpg"
                   //         + "link:" + "http://svr.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="
                   //             + "123"+ "$openId=####";
                   // var vigomessage2 = "title:" + resultwx.message[2].title
                   //         + "desc:" + resultwx.message[2].shortdesc
                   //         + "pic:" + resultwx.message[2].picurl
                   //         + "link:" + resultwx.message[2].linkurl;
                   // var message2 = "title:不可错过的海量专业孕育课程＆胎教早教资源！"
                   //         + "desc:有声资源每周更新，满足妈妈＆宝宝共同需求"
                   //         + "pic:http://admin.mjn.howjoin.com/images/p2.jpg"
                   //         + "link:" + "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ";
                   // var vigomessage3 = "title:" + resultwx.message[3].title
                   //         + "desc:" + resultwx.message[3].shortdesc
                   //         + "pic:" + resultwx.message[3].picurl
                   //         + "link:" + resultwx.message[3].linkurl;
                   // var message3 = "title:【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……"
                   //         + "desc:惊喜连连，准备好了么？"
                   //         + "pic:http://admin.mjn.howjoin.com/images/p3.jpg"
                   //         + "link:" + "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW";


                   // qr_Check qrc = new qr_Check()
                   // {
                   //     qrtype = "88",
                   //     rel_code = "",//table.Rows[i]["nc_code"].ToString(),
                   //     scene_id = "",//table.Rows[i]["scene_id"].ToString(),

                   //     code_url = "",//table.Rows[i]["code_url"].ToString(),
                   //     httpcode_url = "",//qrcodecontent,
                   //     iseqccode_url =false,//(table.Rows[i]["code_url"].ToString() == qrcodecontent),



                   //     message1 = message0,
                   //     vigomessage1 = vigomessage0,
                   //     iseq1 = (message0 == vigomessage0),

                   //     message2 = message1,
                   //     vigomessage2 = vigomessage1,
                   //     iseq2 = (message1 == vigomessage1),

                   //     message3 = message2,
                   //     vigomessage3 = vigomessage2,
                   //     iseq3 = (message2 == vigomessage2),

                   //     message4 = message3,
                   //     vigomessage4 = vigomessage3,
                   //     iseq4 = (message3 == vigomessage3)
                   // };
                   // bool istrue = true;
                   // //if (qr_CheckService.Instance.Add_Entity(qrc))
                   // if (istrue)
                   // {
                   //     txtMessage.Text = "rel_code:" + qrc.rel_code + "code_url:" + qrc.code_url + "iseqccode_url:" + qrc.iseqccode_url + "message1:" + qrc.message1 + "message2:" + qrc.message2
                   //         + "message3:" + qrc.message3 + "message4:" + qrc.message4 + "httpcode_url:" + qrc.httpcode_url;
                   // }
                   // else
                   // {
                   //     txtMessage.Text = "检查NC添加数据失败";
                   // }

                   // #endregion
                    #endregion

                    #region  开始检查004方法
                    //DbSession.Default.FromSql("DELETE qr_check where qrtype=88").Execute();

                    DataTable table = new DataTable();
                    string sql = string.Format(@"SELECT nc.nc_code,qr.scene_id,qr.qrcode_url,qr.code_url,qr.wx_title,qr.wx_content,qr.wx_imgurl,qr.wx_url
from ext_ncuser nc left join qr_QrCodeRelate rel on nc.nc_code=rel.rel_Code 
left join qr_QrCode qr on rel.qr_Scene_ID=qr.scene_id where rel.IsDelete=0 and qr.scene_id={0}", txtScene_id.Text.Trim());

                    table = DbSession.Default.FromSql(sql).ToTable();
                    table.TableName = "checktb";

                    CheckQrcode check = new CheckQrcode();
                    //Qrcode_vigo vigo = new Qrcode_vigo("http://my.erpcoo.com/index.php/Api/Qrcode/index", "sdf5sf5et5e42t5");
                    Qrcode_vigo vigo = new Qrcode_vigo("http://wx.meadjohnson.com.cn/API/Qrcode/index", "sdf5sf5et5e42t5");

                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        try
                        {
                            #region check

                            var imgurl = table.Rows[i]["qrcode_url"].ToString();
                            var qrcodecontent = check.getqrcontent(imgurl);
                            var resultwx = vigo.GetWXContent("", table.Rows[i]["scene_id"].ToString());

                            var vigomessage0 = "title:" + resultwx.message[0].title
                                + "desc:" + resultwx.message[0].shortdesc
                                + "pic:" + resultwx.message[0].picurl
                                + "link:" + resultwx.message[0].linkurl;
                            var message0 = "title:美赞臣蓝臻，突破性贴近宝宝需求，新妈买一赠一限量臻享"
                                    + "desc:"
                                    + "pic:http://qrcode.mjn.howjoin.com/imgforweixin/ac419.jpg"
                                    + "link:" + "http://wx.ismartgo.com/app/lighthouse/index.aspx?from="
                                    + table.Rows[i]["nc_code"].ToString() + "$type=1$openid=####";
                            var vigomessage1 = "title:" + resultwx.message[1].title
                                + "desc:" + resultwx.message[1].shortdesc
                                + "pic:" + resultwx.message[1].picurl
                                + "link:" + resultwx.message[1].linkurl;
                            var message1 = "title:马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇"
                                    + "desc:一键注册，终身礼遇"
                                    + "pic:http://admin.mjn.howjoin.com/images/p4.jpg"
                                    + "link:" + "http://svr.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="
                                        + table.Rows[i]["nc_code"].ToString() + "$openId=####";
                            var vigomessage2 = "title:" + resultwx.message[2].title
                                    + "desc:" + resultwx.message[2].shortdesc
                                    + "pic:" + resultwx.message[2].picurl
                                    + "link:" + resultwx.message[2].linkurl;
                            var message2 = "title:不可错过的海量专业孕育课程＆胎教早教资源！"
                                    + "desc:有声资源每周更新，满足妈妈＆宝宝共同需求"
                                    + "pic:http://admin.mjn.howjoin.com/images/p2.jpg"
                                    + "link:" + "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ";
                            var vigomessage3 = "title:" + resultwx.message[3].title
                                    + "desc:" + resultwx.message[3].shortdesc
                                    + "pic:" + resultwx.message[3].picurl
                                    + "link:" + resultwx.message[3].linkurl;
                            var message3 = "title:【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……"
                                    + "desc:惊喜连连，准备好了么？"
                                    + "pic:http://admin.mjn.howjoin.com/images/p3.jpg"
                                    + "link:" + "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW";


                            qr_Check qrc = new qr_Check()
                            {
                                qrtype = "88",
                                rel_code = table.Rows[i]["nc_code"].ToString(),
                                scene_id = table.Rows[i]["scene_id"].ToString(),

                                code_url = table.Rows[i]["code_url"].ToString(),
                                httpcode_url = qrcodecontent,
                                iseqccode_url = (table.Rows[i]["code_url"].ToString() == qrcodecontent),



                                message1 = message0,
                                vigomessage1 = vigomessage0,
                                iseq1 = (message0 == vigomessage0),

                                message2 = message1,
                                vigomessage2 = vigomessage1,
                                iseq2 = (message1 == vigomessage1),

                                message3 = message2,
                                vigomessage3 = vigomessage2,
                                iseq3 = (message2 == vigomessage2),

                                message4 = message3,
                                vigomessage4 = vigomessage3,
                                iseq4 = (message3 == vigomessage3)
                            };
                            //bool istrue = true;
                            if (qr_CheckService.Instance.Add_Entity(qrc))
                            //if (istrue)
                            {
                                txtMessage.Text = "rel_code:" + qrc.rel_code + "code_url:" + qrc.code_url + "iseqccode_url:" + qrc.iseqccode_url + "message1:" + qrc.message1 + "message2:" + qrc.message2
                                    + "message3:" + qrc.message3 + "message4:" + qrc.message4 + "httpcode_url:" + qrc.httpcode_url;
                            }
                            else
                            {
                                txtMessage.Text = "检查NC添加数据失败";
                            }

                            #endregion

                        }
                        catch (Exception ex)
                        {
                            ComLib.LogLib.Log4NetBase.Log(table.Rows[i]["nc_code"].ToString() + "检查NC错误:" + ex.Message);
                        }

                    }
                    #endregion
                }

                catch (Exception ex)
                {
                    ComLib.LogLib.Log4NetBase.Log("检查NC错误:" + ex.Message);
                }
            }
        }

        //方法001 创建多图文
        protected void Button4_Click(object sender, EventArgs e)
        {
            ComLib.LogLib.Log4NetBase.Log("开始新增一个多图文NC数据！");
            string httpstr = ConfigurationManager.AppSettings["nchttp"];
            string access_key_id = ConfigurationManager.AppSettings["access_key_id"];
            string access_key_secret = ConfigurationManager.AppSettings["access_key_secret"];
            var ncsync = new NC_Sync(httpstr, access_key_id, access_key_secret);

            #region 新增多图文NC数据
            string action = "more";
            string code = "";
            //创建NC二维码
            ComLib.LogLib.Log4NetBase.Log("1、==========================创建多图文NC二维码========开始====");
            string vigohttp = ConfigurationManager.AppSettings["vigohttp"];
            string signscrect = ConfigurationManager.AppSettings["signscrect"];
            NC_QRCode ncqrsync = new NC_QRCode(vigohttp, signscrect);
            string msg = ncqrsync.Sync_QrCodeAdd(action, code);
            txtMessage.Text = msg;
            ComLib.LogLib.Log4NetBase.Log("2、==========================创建多图文NC二维码========结束====");
            #endregion
        }

        //方法003 根据时间段导出二维码扫码记录
        protected void Button1_Click(object sender, EventArgs e)
        {
           #region 得到扫码次数

           // int i = 1;
           // int testpage = 0;
           // int count=0;
           // while (i > 0)
           // {
           //     Qrcode_vigo vigo = new Qrcode_vigo("http://wx.meadjohnson.com.cn/API/Qrcode/index", "sdf5sf5et5e42t5");
           //     var qrlog = vigo.GetUsedCount(i);
           //     //count=qrlog.
           //     //testpage = qrlog.message.pageno;
           //     if (qrlog.message != null)
           //     {
           //         if (qrlog.message.items.Count != 0)
           //         {
           //             var list = qrlog.message.items;
           //             list.ForEach(o =>
           //             {
           //                 if (!qr_LogService.Instance.Get_IsExist_ByWhere(qr_Log._.id == o.id))
           //                 {
           //                     var entity = new qr_Log()
           //                     {
           //                         id = o.id,
           //                         codeid = o.codeid,
           //                         codename = o.codename,
           //                         openid = o.openid,
           //                         addtime = o.addtime
           //                     };

           //                     //qr_LogService.Instance.Add_Entity(entity);
           //                 }
           //                 count += 1;
           //             });

           //             i += 1;
           //         }
           //     }
           //     else
           //     {
           //         i = 0;
           //     }
           // }

           // txtMessage.Text = "正在运行第" + i + "页,总计：" + count + "条数据";
           //// Response.Write("完成" + testpage);
          #endregion            
        }


        #region 医院测试
        //新增单图文医院二维码(接口001)
        protected void Button6_Click(object sender, EventArgs e)
        {

        }
        //新增多图文医院二维码(接口001)
        protected void Button7_Click(object sender, EventArgs e)
        {

        }
        //更新医院二维码(接口002)
        protected void Button8_Click(object sender, EventArgs e)
        {

        }
        #endregion

        protected void Button9_Click(object sender, EventArgs e)
        {
            if (imgurl.Text.Trim().IsNullOrEmpty())
            {
                result.Text = "请输入待解析的二维码路径";
            }
            else
            {
                CheckQrcode check = new CheckQrcode();
                var qrcodecontent = check.getqrcontent(imgurl.Text.Trim());
                result.Text = qrcodecontent;
            }
           
        }
    }
}